provider "alicloud" {
  region = "cn-hangzhou"
}

resource "alicloud_vpc" "vpc" {
  vpc_name       = "tf_test_foo"
  cidr_block = "172.16.0.0/12"
}

resource "alicloud_vswitch" "vsw" {
  vpc_id            = alicloud_vpc.vpc.id
  cidr_block        = "172.16.0.0/21"
  zone_id = var.availability_zone
}

resource "alicloud_security_group" "default" {
  name = var.security_group_name
  vpc_id = alicloud_vpc.vpc.id
}


resource "alicloud_ecs_key_pair" "default" {
  key_pair_name = var.key_name
  public_key    = var.public_key
}

resource "alicloud_instance" "instance" {
//  count                = var.instance_number
  availability_zone = var.availability_zone
  security_groups = alicloud_security_group.default.*.id
  # series III
  instance_type        = var.instance_type
  system_disk_category = "cloud_efficiency"
  image_id             = "centos_8_4_x64_20G_alibase_20210824.vhd"
  instance_name        = var.instance_name
  vswitch_id = alicloud_vswitch.vsw.id
  internet_max_bandwidth_out = 100
//  password            = var.password
  key_name   = alicloud_ecs_key_pair.default.key_pair_name
//  provisioner  "remote-exec" {
//   connection {
//     type = "ssh"
//     host = alicloud_instance.instance.public_ip
//     user = var.user
//     password = var.password
//   }
//   inline = [
//      "export iac_portal=${var.iac_portal}",
//      "export ct_runner=${var.ct_runner}",
//      "export ct_worker=${var.ct_worker}",
//      "export iac_web=${var.iac_web}",
//      "export mysql=${var.mysql}",
//      "export consul=${var.consul}",
//      "export docker_pw=${var.docker_pw}",
//      "curl https://cloudiac.oss-cn-beijing.aliyuncs.com/docker_yunji_deploy.sh | sh",
//   ]
//  }
	
}

resource "alicloud_security_group_rule" "allow_all_tcp" {
  type              = "ingress"
  ip_protocol       = "tcp"
  nic_type          = "intranet"
  policy            = "accept"
  port_range        = "1/65535"
  priority          = 1
  security_group_id = alicloud_security_group.default.id
  cidr_ip           = "0.0.0.0/0"
}

resource "alicloud_dns_record" "record" {
  name        = var.domain_name
  host_record = var.host_record
  type        = "A"
  value       = alicloud_instance.instance.public_ip
}

// 为每个计算资源创建一个对应的 ansible_host 资源，
// 执行 ansible playbook 前会基于 ansible_host 资源自动生成 inventory 文件。
resource "ansible_host" "iac" {
//  count = var.instance_number

  // 配置机器的 hostname，一般配置为计算资源的 public_ip (或 private_ip)
  inventory_hostname = alicloud_instance.instance.public_ip

  // 配置机器所属分组
  groups = ["iac"]

  // 传给 ansible 的 vars，可在 playbook 文件中引用
  vars = {
    wait_connection_timeout = 600
    public_ip = alicloud_instance.instance.public_ip
    private_ip = alicloud_instance.instance.private_ip
    iac_portal=var.iac_portal
    ct_runner=var.ct_runner
    ct_worker=var.ct_worker
    iac_web=var.iac_web
    mysql=var.mysql
    consul=var.consul
    docker_pw=var.docker_pw
    mysql_root_pw=var.mysql_root_pw
  }
}


output "public_ip" {
   value = alicloud_instance.instance.public_ip
}

output "domain_name" {
   value = "${var.host_record}.${var.domain_name}"
}